package com.example.androidsx;

import android.app.Activity;
import android.app.Person;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class UserManagementActivity extends AppCompatActivity {
    private SQLcj dbHelper;
    private ListView listView;
    private SimpleCursorAdapter adapter;
    private TextView username,userpwd,usereml;
    private Button adduser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.usermanagement);


        dbHelper = new SQLcj(this);
        listView = findViewById(R.id.user_list1);
        username=findViewById(R.id.et_username);
        userpwd=findViewById(R.id.et_userpwd);
        usereml=findViewById(R.id.et_usereml);
       adduser=findViewById(R.id.btn_add_user1);

       adduser.setOnClickListener(v -> {
           ShowInputDialog();

       });
        loadUserData();
        findViewById(R.id.btn_back).setOnClickListener(v -> finish());
    }

    private void loadUserData() {
        try {
            Cursor cursor = dbHelper.getAllAccounts();

            String[] fromColumns = {
                    SQLcj.COLUMN_USER_ID,
                    SQLcj.COLUMN_NAME,
                    SQLcj.COLUMN_PWD,
                    SQLcj.COLUMN_EML
            };

            int[] toViews = {
                    R.id.tvid,
                    R.id.tvName,
                    R.id.tvpwd,
                    R.id.tvEml
            };


            adapter = new SimpleCursorAdapter(
                    this,
                    R.layout.listview_user,
                    cursor,
                    fromColumns,
                    toViews,
                    0
            );


            listView.setAdapter(adapter);


            listView.setOnItemClickListener((parent, view, position, id) -> {
                Cursor item = (Cursor) adapter.getItem(position);
                if (item.moveToPosition(position)) {
                    String userName = item.getString(item.getColumnIndexOrThrow(SQLcj.COLUMN_NAME));
                    Toast.makeText(this, "选中用户: " + userName, Toast.LENGTH_SHORT).show();
                }
            });

        } catch (Exception e) {
            Toast.makeText(this, "加载用户数据失败", Toast.LENGTH_SHORT).show();
            Log.e("UserManagement", "数据库错误: ", e);
        }
    }

    private void ShowInputDialog() {
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialong_adduser, null);


        TextView dialogUsername = dialogView.findViewById(R.id.et_username);
        TextView dialogUserpwd = dialogView.findViewById(R.id.et_userpwd);
        TextView dialogUsereml = dialogView.findViewById(R.id.et_usereml);

        new AlertDialog.Builder(this)
                .setTitle("输入用户信息")
                .setView(dialogView)
                .setPositiveButton("保存", (dialogInterface, which) -> {
                    String name = dialogUsername.getText().toString();
                    String pwd = dialogUserpwd.getText().toString();
                    String eml = dialogUsereml.getText().toString();

                    try {
                        long id = dbHelper.addAccount(null, name, pwd, eml);
                        if (id != -1) {
                            Toast.makeText(this, "用户添加成功", Toast.LENGTH_SHORT).show();
                            loadUserData();
                        }
                    } catch (Exception e) {
                        Toast.makeText(this, "输入有效信息", Toast.LENGTH_SHORT).show();
                        Log.e("UserManagement", "添加用户错误", e);
                    }
                })
                .setNegativeButton("取消", null)  // 使用setNegativeButton而不是第二个setPositiveButton
                .show();
    }

    @Override
    protected void onDestroy() {
        if (adapter != null) {
            Cursor cursor = adapter.getCursor();
            if (cursor != null) cursor.close();
        }
        if (dbHelper != null) dbHelper.close();
        super.onDestroy();
    }
}